# -*- coding:UTF-8 -*-
"""
@Project : pythonProject
@File    : decorator_calc.py
@IDE     : PyCharm
@Author  : WorldWind(1772830365@qq.com)
@Address : China - HeiLongjiang - Herbin
@Date    : 2025/7/15 下午2:11
"""
from time import sleep, time


# 定义一个装饰器,本身就是一个函数
# 参数是另一个函数
def decorator(func):
    def new_func(*args, **kwargs):
        print('>>>>>>>>>>>>>>>')
        beginTime = time()
        result = func(*args, **kwargs)  # 执行外部传入的函数,接收返回值
        endTime = time()
        print(f'{func.__name__},运行时间:{round(endTime - beginTime, 2)}s')
        print('<<<<<<<<<<<<<<<')
        return result

    return new_func


def test(func):
    def new_func(*args, **kwargs):
        func(*args, **kwargs)
        print("多个装饰器的应用")


# 要求:统计一下每个方法执行消耗的时间
class Calc:
    def __init__(self):
        print(f'This is __init__ method. {self.get_class_name()} class startup')

    @decorator
    def meth1(self):
        print('m1...')
        sleep(1)

    @decorator
    def meth2(self):
        print('m2...')
        sleep(2)

    @decorator
    def meth3(self):
        print('m3...')
        sleep(3)

    @classmethod
    def get_class_name(cls):
        return cls.__name__
    @decorator
    def add(self, a, b):
        return a + b

    def __del__(self):
        print(f'This is __del__ method. The {self.get_class_name()} class has been recycled')


if __name__ == '__main__':
    c = Calc()
    # c.meth1()
    # c.meth2()
    # c.meth3()
    print(c.add(1,2))
    # beginTime = time()
    # c.meth1()
    # endTime = time()
    # print(f'm1运行时间:{round(endTime - beginTime, 2)}')
    # beginTime = time()
    # c.meth2()
    # endTime = time()
    # print(f'm2运行时间:{round(endTime - beginTime, 2)}')
    # beginTime = time()
    # c.meth3()
    # endTime = time()
    # print(f'm3运行时间:{round(endTime - beginTime, 2)}')
